<?php

//	NOTE: this is used when ePay uses it's callback

// NOTE: for testing: http://localhost/wp/index.php/sppaymentconfirmation?test=

//	require_once "ttfOrderDatabase.php";

	get_header();
	
	$dbLog = new ttfPaymentCallbackLogsDatabase;
	$dbLog->setValue( "date", "coming" );

	$log = serialize( $_REQUEST );

	
	$btest = isset($_REQUEST["test"]) ? true:false;

	if ( !$btest )
	{
		$params = $_GET;
		$var = "";

		foreach ($params as $key => $value)
		{
			if($key != "hash")
			{
				$var .= $value;
			}
		}

		$genstamp = md5($var . "rogerrabbit");
		if ( !isset($_GET["hash"] ) || $genstamp!=$_GET["hash"] )
		{
			echo "Hash is not valid";
			$log.= "\n\nHash is not valid, exiting early";
			$dbLog->setValue( "information", $log );
			$dbLog->save();
			exit();
		}
		
		// Hash is OK    
		$log.= "\n\nHash is good setting status to complete for order";
	
		// change order payment status to: Completed
		$orderid 		= $_REQUEST["orderid"];
		$dbOrder 		= new ttfOrdersDatabase( $orderid );
		$dbOrder->setPaymentStatus( spOrderDatabase::PAYMENT_STATUS_COMPLETED );
		$dbOrder->save();
	}
	
	/////////////////////////////////
	// invoice stuff here
	/////////////////////////////////

	if ( !$btest )
	{
		$memberid 		= $dbOrder->getValue( "memberid" );
		$merchandiseid 	= $dbOrder->getValue( "merchandiseid" );
		
		$dbMerchandise = new ttfMerchandisesDatabase( $merchandiseid );
		
		$dbMembers = new ttfMembersDatabase( $memberid );
		$fullname 	= $dbMembers->getFullName();
		$address	= $dbMembers->getValue( "address" );
		$email 		= $dbMembers->getValue( "email" );
		$telephone 	= $dbMembers->getValue( "telephone" );
		
		$orderTitle 		= $dbMerchandise->getValue( "title" );
		$orderDescription 	= $dbMerchandise->getValue( "description" );
		$orderPrice 		= $dbMerchandise->getValue( "price" );
		
		$orderDate			= $dbOrder->getValue( "date" );
		$orderNumber		= $dbOrder->getValue( "id" );
	}
	else
	{
		$fullname = "Peter Wraae Marino";
		$address = "Gyvelvej 8";
		$email = "marino.peter@gmail.com";
		$telephone = "23311833";
		$orderTitle = "Kontingent Trin 2";
		$orderDescription = "This is the order description";
		$orderPrice = "1000";
		$orderDate = "17-01-1969";
		$orderNumber = "0012300xx";
	}
	
	$invoice = new spInvoice(
		$fullname,
		$address,
		$email,
		$telephone,
		$orderTitle,
		$orderDescription,
		$orderPrice,
		$orderDate,
		$orderNumber
	);
	
	// html
	$html = new spHtml;
	$html->addChild( $head = new spHead );
	$html->addChild( $body = new spBody );
	
	$head->addChild( new spMeta(array(
		"http-equiv" 	=> "Content-Type",
		"content"		=> "text/html; charset=utf-8"
	)));
	$head->addChild( new spTitle(array(
		"content" => "Tumbling Team Fyn"
	)));
	
	$body->addChild( $invoice );
	
	if ( !$btest )
	{
		$email = new spEmail(array(
			"to" 		=> $email,
			"subject" 	=> "Tumbling Team Fyn Ordrer",
			"from" 		=> "Tumbling Team Fyn <noreply@tumblingteamfyn.dk>",
			"message" 	=> $html
		));
		$email->send();
	}
	else
		$html->render();
		
	$dbLog->setValue( "information", $log );
	$dbLog->save();
	
	wp_footer();
?>